package top.wardyan.wtask.feishu.vo;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;

import com.jfinal.aop.Aop;
import com.jfinal.plugin.activerecord.Record;

import cn.hutool.core.lang.Console;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.NumberUtil;
import top.wardyan.common.util.DateUtil;
import top.wardyan.wtask.service.IProcService;
public class FeishuTask {

	private static final cn.hutool.log.Log log = cn.hutool.log.Log.get();

	
	String man;
	Timestamp date;
	
	Float totalHour;
	List<FeishuTaskItem> items;
	
	
/**
	public static TapdMsg parse(String msg) {
		TapdMsg rs = new TapdMsg();
		
		// 消息格式:from=冯超华&msg=2024-11-23 周六
		// 兴雪莱 - 【ID1025698】 （加急）天泽吊挂对接（吊挂线号区分） 2 未完成
		// 兴雪莱 - 【ID1025803】 扎卡生成规则调整 2.5
		// 兴雪莱 - 【ID1025460】 pda收料赋码增加公司限制 1
		// ReUtil.findAll("", msg);
		// ReUtil.getAllGroups("", msg);
		Pattern pattern = Pattern.compile("from=(.*)&msg=(.*)", Pattern.DOTALL);
		List<String> lines = ReUtil.getAllGroups(pattern, msg, false);
		// lines:
		// 1姓名
		// 2日期等

		if(lines == null) {
			lines = new ArrayList<>();
		}
		
		if(lines != null && lines.size() > 1) {
			
			rs.setFrom(lines.get(0));
			String msglines = lines.get(1);
			if(msglines == null) {
				msglines = "";
			}
			
//			Pattern linepaggern = Pattern.compile("from=(.*)&msg=(.*)", Pattern.DOTALL);
//			List<String> tasklines = ReUtil.getAllGroups(linepaggern, msglines, false);
			String[] tasklinestrs = msglines.split("\n");
			
			if(tasklinestrs != null && tasklinestrs.length > 0) {
				String firstline = tasklinestrs[0];
				String[] fs = firstline.split(" ");		//有些日期行会带星期 说的就是我
				if(fs != null && fs.length > 0) { 
					firstline = fs[0];
				}
				rs.setDate(DateUtil.parse(firstline));
				
				tasklinestrs = ArrayUtil.sub(tasklinestrs, 1, tasklinestrs.length + 10);
			}
			List<TapdMsgLine> gns = new ArrayList<>();
			
			for(String line : tasklinestrs) {
				TapdMsgLine gn = TapdMsgLine.parse(line);
				gns.add(gn);
			}
			rs.setLines(gns);
		}
		
		return rs;
	}
**/	
	/**
	 * 解析消息FeishuTask.
	 * 格式: 
	 * 第一行固定是: 日期 [星期] [总工时]
	 * 第二行以后:
	 * [客户 - ][【IDTAPDID】]名称 工时h [未完成]
	 * @param text
	 * @return
	 */
	@SuppressWarnings("null")
	public static FeishuTask parse(String text) {
		FeishuTask rs = new FeishuTask();
		
		if(text == null) {
			text = "";
		}
		text = text.replace("<p>", "");
		text = text.replace("</p>", "\n");
		String[] tasklinestrs = text.split("\n");
		
		if(tasklinestrs != null && tasklinestrs.length > 0) {
			String firstline = tasklinestrs[0];
			firstline = firstline.replace("\t", " ");
			firstline = firstline.replace(":", " ");
			firstline = firstline.replace("：", " ");
			firstline = firstline.replace("<p>", "");
			firstline = firstline.replace("</p>", "");
			firstline = firstline.trim();
			
			String[] fs = firstline.split(" ");		//有些日期行会带星期 说的就是我
			if(fs != null && fs.length > 0) { 
				firstline = fs[0];
				
				// 如果最后面一位可以转换为浮点值,就作为总工时. 否则从明细表汇总上来
				String th = fs[fs.length - 1];
				th = th.toLowerCase().replace("h", "");

//				if(text.contains("1027652")){
//					Console.log(text);
//				}

				if(NumberUtil.isInteger(th)) {
					rs.setTotalHour(NumberUtil.parseInt(th));
				}else if(NumberUtil.isDouble(th)) {
					rs.setTotalHour(NumberUtil.parseDouble(th));
				}
				
//				if(text.contains("1027652")){
//					Console.log(text);
//				}
				
				
			}
			Timestamp date = DateUtil.parse(firstline);		// 最前面一部分作为日期
//			if(date == null) {
//				Console.log(date);
//			}
			rs.setDate(date);
			
			// 如果倒数第二部分可以识别为一个人...
			if(fs.length > 2) {
				String ss = fs[fs.length - 2];
				
				if(ss != null) {
					IProcService procService = Aop.get(IProcService.class);
					List<Record> es = procService.yp_listempls(true);
					if(es != null) {
						Optional<Record> ee =  es.stream().filter(new Predicate<Record>() {

							@Override
							public boolean test(Record t) {
								return ss.equals(t.getStr("xName"));
							}
							
						}).findFirst();
						if(ee.isPresent()) {
							Record e = ee.get();
							rs.setMan(e.getStr("xName"));
						}
					}
					
				}
			}
			
			
			tasklinestrs = ArrayUtil.sub(tasklinestrs, 1, tasklinestrs.length + 10);
		}
		List<FeishuTaskItem> gns = new ArrayList<>();
		
		for(String line : tasklinestrs) {
			line = line.toLowerCase();
			FeishuTaskItem gn = FeishuTaskItem.parse(line);
			gns.add(gn);
		}
		
		rs.setItems(gns);
		
		if((rs.getTotalHour() == null || rs.getTotalHour() <= 0) && gns != null && gns.size() > 0) {
			Float totalhour = 0.0F;
			for(FeishuTaskItem item : gns) {
				totalhour += item.getHour();
			}
			rs.setTotalHour(totalhour);
		}
		return rs;
	}
	

	public String getMan() {
		return man;
	}

	public FeishuTask setMan(String man) {
		this.man = man;
		return this;
	}

	public Timestamp getDate() {
		return date;
	}

	public FeishuTask setDate(Timestamp date) {
		this.date = date;
		return this;
	}

	public List<FeishuTaskItem> getItems() {
		return items;
	}

	public FeishuTask setItems(List<FeishuTaskItem> items) {
		this.items = items;
		return this;
	}


	public Float getTotalHour() {
		return totalHour;
	}


	public FeishuTask setTotalHour(Float totalHour) {
		this.totalHour = totalHour;
		return this;
	}

	public FeishuTask setTotalHour(Integer totalHour) {
		this.totalHour = totalHour * 1.0F;
		return this;
	}

	public FeishuTask setTotalHour(Double totalHour) {
		this.totalHour = totalHour.floatValue();
		return this;
	}
	
	
	
	
	
}
